home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / kr.arc / C_FUNCT.C next >
Text File  |  1985-06-28  |  10KB  |  1,323 lines

  1. pr *.f
  2.  
  3.  
  4. Jul 27 17:12 1984  fact.f Page 1
  5.  
  6.  
  7. double fact(num)        /* recursive function to compute and
  8.                            return factorial (num!) for num > 0 */
  9. int num;
  10. {       if      (num > 1)
  11.                 return (num * fact(num - 1));
  12.         else    return 1;
  13. }
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. Jul 27 17:12 1984  getdbl.f Page 1
  71.  
  72.  
  73. double  getdbl()        /* convert ASCII from terminal to dbl */
  74. {       int     c, sign = 1;    /* c = char input, sign = + */
  75.         float   d = 1.0;        /* d = decimal place */
  76.         double  num = 0.0;      /* num = converted number */
  77.         printf("input number: ");
  78.         if      ((c = getchar()) == '-')
  79.                 sign = -1;                      /* sign = - */
  80.         else if (c == '\n' || c == EOF)
  81.                 return (0);
  82.         else if (c == '.')
  83.                 goto dot;
  84.         else if (c >= '0' && c <= '9')
  85.                 num = (c - '0');
  86.         while ((c = getchar()) != '\n' && c != '.' && c != EOF)
  87.                 if (c >= '0' && c <= '9')
  88.                         num = num * 10 + (c - '0');
  89.         if (c == '.')
  90. dot:            while ((c = getchar()) != EOF && c != '\n')
  91.                         if (c >= '0' && c <= '9')
  92.                         { d /= 10.0; num += d * (c - '0');
  93.                         }
  94.         return (num * sign);
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. Jul 27 17:12 1984  getdbl_a.f Page 1
  137.  
  138.  
  139. double getdbl_a()       /* use (3S) & (3C) functions */
  140. {       char stor[MAX];
  141.         double atof();
  142.  
  143.         printf("input number: ");
  144.         return (atof(fgets(stor, MAX, stdin)));
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. Jul 27 17:12 1984  getdbl_n.f Page 1
  203.  
  204.  
  205. double  getdbl_n()      /* convert ASCII to dbl - 2nd version */
  206. {       int     c, sign = 1;    /* c = char input, sign = + */
  207.         float   d = 1.0;        /* d = decimal place */
  208.         double  num = 0.0;      /* num = converted number */
  209.         printf("input number: ");
  210.         if      ((c = getc(stdin)) == '-')
  211.                 sign = -1;              /* sign = - */
  212.         else if (c == '\n' || c == EOF)
  213.                 return (0);
  214.         else if (c == '.' || (c >= '0' && c <= '9'))
  215.                 ungetc(c, stdin);       /* return c to buffer */
  216.         while ((c = getc(stdin)) != '\n' && c != '.' && c != EOF)
  217.                 if (c >= '0' && c <= '9')
  218.                         num = num * 10 + (c - '0');
  219.         if (c == '.')
  220.                 while ((c = getc(stdin)) != EOF && c != '\n')
  221.                         if (c >= '0' && c <= '9')
  222.                         { d /= 10.0; num += d * (c - '0');
  223.                         }
  224.         return (num * sign);
  225. }
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. Jul 27 17:12 1984  getint.f Page 1
  269.  
  270.  
  271. getint()        /* function to convert ASCII
  272.                 /* input from terminal to an int */
  273. {       int     c, sign = 1, num = 0;
  274.  
  275.         printf("input integer: ");
  276.         if      ((c = getchar()) == '-')
  277.                 sign = -1;
  278.         else if (c >= '0' && c <= '9')
  279.                 num = (c - '0');
  280.         else
  281.                 return (0);
  282.         while ((c = getchar()) >= '0' && c <= '9')
  283.                 num = num * 10 + (c - '0');
  284.         return (num * sign);
  285. }
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. Jul 27 17:12 1984  getintp.f Page 1
  335.  
  336.  
  337. int *getintp()  /* function to convert ASCII
  338.                 /* input from terminal to an int;
  339.                 /* return pointer to int */
  340. { int   c, sign = 1, num = 0;
  341.  
  342.   printf("input integer: ");
  343.   if    ((c = getchar()) == '-')
  344.         sign = -1;
  345.   else if (c >= '0' && c <= '9')
  346.         num = (c - '0');
  347.   else
  348.         return (&num);
  349.   while ((c = getchar()) >= '0' && c <= '9')
  350.         num = num * 10 + (c - '0');
  351.   num *= sign;
  352.   return (&num);
  353. }
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. Jul 27 17:12 1984  getline.f Page 1
  401.  
  402.  
  403. getline(line, lim)      /* function to read a line from std input;
  404.                         /* insert NULL at end and return char count */
  405. char    line[];         /* array where line will be stored */
  406. int     lim;            /* max line size */
  407. {
  408.   int   c, i;
  409.  
  410.   for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
  411.         line[i] = c;
  412.   if (c == '\n')                /* retain  */
  413.         line[i++] = c;          /* newline */
  414.   line[i] = '\0'; return (i);
  415. }
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. Aug  3 14:21 1984  getlinep.f Page 1
  467.  
  468.  
  469. getlinep(line, lim)     /* function to read a line from std input;
  470.                         /* insert NULL at end and return char count */
  471. char    *line;          /* array where line will be stored */
  472. int     lim;            /* max line size */
  473. {
  474.   int   c, i;
  475.  
  476.   for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
  477.         *line++ = c;
  478.   if (c == '\n')                /* retain  */
  479.         *line++ = c;            /* newline */
  480.   *line = '\0'; return (i);
  481. }
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. Jul 27 17:12 1984  getlinepn.f Page 1
  533.  
  534.  
  535. getlinepn(pline, lim)   /* function to get a line from std input
  536.                         /* (do not save newline); insert null at end
  537.                         /* and return char count per PA ex. 04a */
  538. char    *pline;         /* pointer to array where line will be stored */
  539. int     lim;            /* max line size */
  540. {
  541.   register c, i;
  542.  
  543.   for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
  544.         *pline++ = c;           /* newline is                   */
  545.   *pline = '\0'; return (i);    /*      not retained or counted */
  546. }
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. Jul 27 17:12 1984  inter.f Page 1
  599.  
  600.  
  601. inter(pfi)      /* call function via pointer arg */
  602. int (*pfi)();   /* pfi is pointer to function
  603.                 /* returning type int */
  604. {
  605.   return ((*pfi)());    /* (*pfi)() is a function call */
  606. }
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. Jul 27 17:12 1984  itob.f Page 1
  665.  
  666.  
  667. itob(num, ar)   /* convert int to ASCII 0's and 1's */
  668. int num;        /* received int */
  669. char ar[];      /* array to store 0's and 1's */
  670. {       int cnt, mask = 1;
  671.         for (cnt = (8 * sizeof(cnt)) - 1; cnt >= 0; cnt--)
  672.         {       ar[cnt] = ((num & mask)? '1': '0'); mask <<= 1;
  673.         }
  674.         ar[(8 * sizeof(cnt))] = '\0';
  675. }
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730. Jul 27 17:12 1984  power.f Page 1
  731.  
  732.  
  733. power(x, n)     /* raise x to the n-th power; n > 0 */
  734. int x, n;       /* declaration of arguments */
  735. {
  736.         int i, p = 1;
  737.         for (i = 1; i <= n; ++i)
  738.                 p = p * x;
  739.         return (p);
  740. }
  741.  
  742.  
  743.  
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.  
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796. Jul 27 17:12 1984  square.f Page 1
  797.  
  798.  
  799. square(ar, sz)  /* fills array ar of size sz with the
  800.                 /* square of the element number */
  801. int ar[], sz;
  802. {
  803.         for (--sz; sz >= 0; --sz)
  804.                 ar[sz] = sz * sz;
  805. }
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862. Jul 27 17:12 1984  strcmp.f Page 1
  863.  
  864.  
  865. strcmp(s1, s2)  /* compare 2 strings;
  866.                 /* if match return 0 (else char count) */
  867. char    *s1, *s2;
  868. {       register i;
  869.         for (i = 1; *s1 == *s2; ++i)
  870.                 if (*s1++ == '\0')
  871.                         return (0);
  872.                 else s2++;
  873.         return (i);
  874. }
  875.  
  876.  
  877.  
  878.  
  879.  
  880.  
  881.  
  882.  
  883.  
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928. Jul 27 17:12 1984  strcpy.f Page 1
  929.  
  930.  
  931. strcpy(d, s)    /* copy string in s[] into d[] */
  932. char d[], s[];
  933. {       register i;
  934.         for (i = 0; d[i] = s[i]; ++i)
  935.                 ;
  936. }
  937.  
  938.  
  939.  
  940.  
  941.  
  942.  
  943.  
  944.  
  945.  
  946.  
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994. Jul 27 17:12 1984  strcpy_p.f Page 1
  995.  
  996.  
  997. strcpy_p(d, s)  /* copy string *s to *d */
  998. char *d, *s;
  999. {       
  1000.         while (*d++ = *s++)
  1001.                 ;
  1002. }
  1003.  
  1004.  
  1005.  
  1006.  
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.  
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060. Jul 27 17:12 1984  strln.f Page 1
  1061.  
  1062.  
  1063. strln(str)      /* return length of char string stored in array str;
  1064.                 /* include null at end in count */
  1065. char str[];
  1066. {       int i = 0, length = 0;
  1067.         do
  1068.         {       ++length;
  1069.         } while (str[i++] != '\0');
  1070.         return (length);
  1071. }
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126. Jul 27 17:12 1984  strln_p.f Page 1
  1127.  
  1128.  
  1129. strln_p(ptr)    /* return length of char string pointed to by ptr;
  1130.                 /* include null at end in count */
  1131. char *ptr;
  1132. {       register length = 0;
  1133.         do
  1134.         {       ++length;
  1135.         } while (*ptr++ != '\0');       /* ++ increments ptr by 1 */
  1136.         return (length);
  1137. }
  1138.  
  1139.  
  1140.  
  1141.  
  1142.  
  1143.  
  1144.  
  1145.  
  1146.  
  1147.  
  1148.  
  1149.  
  1150.  
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180.  
  1181.  
  1182.  
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192. Jul 27 17:12 1984  swap.f Page 1
  1193.  
  1194.  
  1195. swap(px, py)            /* call by reference */
  1196. int *px, *py;           /* px, py are pointers to type int */
  1197. {       int tmp;
  1198.         tmp = *px;
  1199.         *px = *py;
  1200.         *py = tmp;
  1201. }
  1202.  
  1203.  
  1204.  
  1205.  
  1206.  
  1207.  
  1208.  
  1209.  
  1210.  
  1211.  
  1212.  
  1213.  
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258. Jul 27 17:12 1984  vol.f Page 1
  1259.  
  1260.  
  1261. double vol(a, b, c)     /* given 3 dimensions compute volume */
  1262. double a, b, c;         /* arg declarations */
  1263. {
  1264.         return (a * b * c);
  1265. }
  1266.  
  1267.  
  1268.  
  1269.  
  1270.  
  1271.  
  1272.  
  1273.  
  1274.  
  1275.  
  1276.  
  1277.  
  1278.  
  1279.  
  1280.  
  1281.  
  1282.  
  1283.  
  1284.  
  1285.  
  1286.  
  1287.  
  1288.  
  1289.  
  1290.  
  1291.  
  1292.  
  1293.  
  1294.  
  1295.  
  1296.  
  1297.  
  1298.  
  1299.  
  1300.  
  1301.  
  1302.  
  1303.  
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.